package org.example.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.example.tms.config.AjaxResult;
import org.example.tms.mapper.ClazzMapper;
import org.example.tms.mapper.EmpMapper;
import org.example.tms.pojo.Clazz;
import org.example.tms.pojo.Emp;
import org.example.tms.pojo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private EmpMapper empMapper;

    @RequestMapping
    public AjaxResult getClazzs(String name,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                                @RequestParam(defaultValue = "1",name="page") Integer pageNum,
                                @RequestParam(defaultValue = "1") Integer pageSize){
        Page<Clazz> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(name!=null&& !name.isEmpty(),"name",name);
        queryWrapper.between(begin!=null&&end!=null,"end_date",begin,end);
        clazzMapper.selectPage(page, queryWrapper);

        for(Clazz clazz : page.getRecords()){
            Emp emp = empMapper.selectById(clazz.getMasterId());
            clazz.setMasterName(emp.getName());

            LocalDate today = LocalDate.now();
            if (clazz.getBeginDate().isAfter(today)){
                clazz.setStatus("未开班");
            }else if (clazz.getEndDate().isBefore(today)){
                clazz.setStatus("已结课");
            }else
            {clazz.setStatus("已开班");}
            System.out.println(clazz.getStatus());
        }

        PageBean pageBean = new PageBean();
        pageBean.setRows(page.getRecords());
        pageBean.setTotal(page.getTotal());

        return AjaxResult.success(pageBean);

    }
    @DeleteMapping("/{id}")
    public AjaxResult deleteClazz(@PathVariable String id){
        clazzMapper.deleteById(id);
        return AjaxResult.success();
    }
    @PostMapping
    public AjaxResult saveClazz(@RequestBody Clazz clazz){
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
        return AjaxResult.success();
    }
    @GetMapping("/{id}")
    public AjaxResult getClazzById(@PathVariable Integer id){
        Clazz clazz = clazzMapper.selectById(id);
        return AjaxResult.success(clazz);
    }
    @PutMapping
    public AjaxResult updateClazz(@RequestBody Clazz clazz){
        clazzMapper.updateById(clazz);
        return AjaxResult.success();
    }
    @GetMapping("/list")
    public AjaxResult getClazzList(){
        List<Clazz> list = clazzMapper.selectList(null);
        return AjaxResult.success(list);
    }

}
